home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / iaca101.zip / MASTENV.ASM < prev   
Assembly Source File  |  1988-09-04  |  4KB  |  122 lines

  1. ;
  2. ;  Downloaded from BIX
  3. ;
  4. ;=========================
  5. ;tech.notes/pc.code #29, from pmaupin, 3407 chars, Sat Jun  4 22:40:45 1988
  6. ;--------------------------
  7. ;TITLE: Finding DOS's master environment pointer
  8. ;This is a fragment of code that my SD.COM program uses to find
  9. ;the environment.  This fragment is different than most ways of
  10. ;finding the environment, in that it finds the MASTER environment block,
  11. ;not the current process's parent's environment.
  12.  
  13. ;This is useful in some cases, and has the added advantage that
  14. ;it does NOT behave differently when executing under CodeView,
  15. ;so you do NOT have to hard-code your system's DOS environment address
  16. ;into your program in order to debug it.
  17.  
  18. ; Modified by JXL 9/3/88:
  19. ;
  20. ;    Name changed to findenv.
  21. ;
  22. ;    Changed to a FAR proc.  Segment-modeled for MSC or TC large mem model
  23. ;
  24. ;    Returns the segment-pointer to BLOCK HEADER OF the master environment
  25. ;    in AX, thus making it an int func for calling by C.
  26. ;
  27. ;    Returns NULL if the Master environment cannot apparently
  28. ;    be found.
  29. ;
  30. ;    Saves and restores all registers.
  31. ;
  32. ;    Makes no assumption about the initial contents of any register.
  33.  
  34. findenv_text segment word public 'code'
  35.     assume cs:findenv_text
  36.  
  37. EnvPtr             EQU       2CH       ; Offset in PSP
  38.  
  39. CommandInterrupt   EQU       2EH       ; entry point into first Command.Com
  40.                                        ; through interpreter
  41.  
  42. DosSegPtr          EQU       CommandInterrupt * 4 + 2
  43.  
  44.  
  45. ; FindEnvironment is passed:
  46.  
  47. ;   DS should point to program PSP
  48.  
  49. ;    JXL mod: NO! not passed anything at all.
  50.  
  51. ; FindEnvironment returns:
  52.  
  53. ;   ES points to master environment block, or program's copy if couldn't
  54. ;              find the master.
  55.  
  56. ;    JXL mod: returns 0 if couldn't find master.
  57. ;    JXL mod: restores ES; returns segment of BLOCK HEADER in AX
  58. ;             if master is found.
  59.  
  60. ;   CX is length of block, or 0 if couldn't find the master.
  61.  
  62. ;    JXL mod: does not return the length; CX is restored.
  63.  
  64. ; FindEnvironment destroys:
  65.  
  66. ;   AX, SI
  67.  
  68. ;    JXL mod: does not destroy anything except AX.
  69.  
  70. public            _findenv
  71. _findenv            PROC  FAR 
  72.                 push es
  73.                 push si
  74.                 push cx
  75.                    xor   si,si                ; Point to segment 0
  76.                    mov   es,si
  77.                    mov   si, word ptr es:[DosSegPtr]
  78.                    mov   ax,si
  79.                    call  VerifyBlock          ; make sure we've found COMMAND
  80.                    jnz   GotBlock             ; jump if not a good block --
  81.  
  82.                    mov   ax,es:[EnvPtr+10h]   ; get COMMAND's environment ptr
  83.                    or    ax,ax                ; jump if COMMAND has a
  84.                    jnz   MaybeGoodBlock       ; subsidiary environment
  85.  
  86.                    mov   ax,si                ; If no subsidiary, just use
  87.                    add   ax,cx                ; the allocation block
  88.                    inc   ax                   ; immediately after COMMAND
  89.  
  90. MaybeGoodBlock:    call  VerifyBlock          ; verify that we have a good
  91.                                               ; one, one way or another
  92. GotBlock:
  93.                 pop cx
  94.                 pop si
  95.                 pop es
  96.                    ret
  97.  
  98.  
  99. ; VerifyBlock tries to insure that we're pointing to a valid DOS
  100. ; allocation block.  If not, returns the current process's environment
  101. ; block.  [JXL mod: returns 0 in AX instead]
  102.  
  103.  
  104. VerifyBlock        PROC  NEAR
  105.                    dec   ax                      ; get block header into ES
  106.                    mov   es,ax
  107.                    cmp   byte ptr es:[0],04Dh    ; make sure signature is valid
  108.                    jnz   UseCurrent
  109.                    cmp   word ptr es:[1],si      ; make sure owner is valid
  110.                    jnz   UseCurrent
  111.                 mov cx,word ptr es:[3]            ; retrieve the length
  112.                    ret
  113.  
  114. UseCurrent:        mov   ax,0                    ; set failure
  115.                    ret
  116. VerifyBlock        ENDP
  117.  
  118. _findenv           ENDP
  119. findenv_text        ends
  120.  
  121.                     END
  122.